﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
namespace RpgEditor
{
    public class Item
    {
        static SByte TYPE_DRUG = 0;
        static SByte TYPE_WEAPON = 1;
        static SByte TYPE_EQUIP = 2;
        static SByte TYPE_JEWELRY = 3;
        static SByte TYPE_MISSION = 4;
        //#if TW
        //# static String[] JOB = {"戰士","法師","牧師"};
        //#else
        static String[] JOB = { "战士", "法师", "牧师" };
        //#endif

        SByte id;

        String name;

        String info;

        short money;

        /** 职业要求 */
        SByte job;

        /** 等级要求 */
        SByte level;

        /** 物品类型 */
        SByte type;

        /** 掉落概率 */
        SByte pro;

        /** 智力加成 */
        short _int;

        short _pow;

        short _mov;

        short _pyh;

        short _pact;

        short _pdef;

        short _mact;

        short _mdef;

        short _hp;

        short _mp;

        SByte goal;

        SByte state;

        SByte stateLastTime;

        public ArrayList data = new ArrayList();
        mainForm form = null;
        private String fn;
        public Item(mainForm f)
        {
            form = f;
        }
        public void setFn(String fn)
        {
            this.fn = fn;
            form.textBox34.Text = fn;
        }

        public String getFn()
        {
            return fn;
        }

        public void init()
        {
            data = new ArrayList();
        }

        public void save()
        {
            FileStream fs1 = new FileStream(fn, FileMode.Create, FileAccess.Write);
            BinaryWriter dis = new BinaryWriter(fs1, System.Text.Encoding.Default);

            MyIO.writeByte(dis, (SByte)data.Count);

            for (int i = 0; i < data.Count; i++)
            {
                Item it = (Item)data[i];
                MyIO.writeByte(dis, it.id);
                MyIO.writeString(dis, it.name);
                MyIO.writeByte(dis, it.type);
                MyIO.writeByte(dis, 0); // ignore icon
                MyIO.writeInt16(dis, it.money);
                MyIO.writeByte(dis, it.job);
                MyIO.writeByte(dis, it.level);
                MyIO.writeString(dis, it.info);
                MyIO.writeByte(dis, it.pro);

                MyIO.writeInt16(dis, it._hp);
                MyIO.writeInt16(dis, it._mp);
                MyIO.writeInt16(dis, it._pact);
                MyIO.writeInt16(dis, it._mact);
                MyIO.writeInt16(dis, it._pdef);
                MyIO.writeInt16(dis, it._mdef);

                MyIO.writeInt16(dis, it._pow);
                MyIO.writeInt16(dis, it._pyh);
                MyIO.writeInt16(dis, it._int);
                MyIO.writeInt16(dis, it._mov);

                MyIO.writeByte(dis, it.goal);
                MyIO.writeByte(dis, it.state);
                MyIO.writeByte(dis, it.stateLastTime);
            }

            dis.Close();

        }

        public void getitemInfo(String fn)
        {

            FileStream fs1 = new FileStream(fn, FileMode.Open, FileAccess.Read);
            BinaryReader dis = new BinaryReader(fs1, System.Text.Encoding.Default);

            int num = MyIO.readByte(dis);

            data = new ArrayList();

            for (int i = 0; i < num; i++)
            {
                Item it = new Item(null);
                it.id = MyIO.readByte(dis);
                it.name = MyIO.readString(dis);
                it.type = MyIO.readByte(dis);
                MyIO.readByte(dis); // ignore icon
                it.money = MyIO.readInt16(dis);
                it.job = MyIO.readByte(dis);
                it.level = MyIO.readByte(dis);
                it.info = MyIO.readString(dis);
                it.pro = MyIO.readByte(dis);

                it._hp = MyIO.readInt16(dis);
                it._mp = MyIO.readInt16(dis);
                it._pact = MyIO.readInt16(dis);
                it._mact = MyIO.readInt16(dis);
                it._pdef = MyIO.readInt16(dis);
                it._mdef = MyIO.readInt16(dis);

                it._pow = MyIO.readInt16(dis);
                it._pyh = MyIO.readInt16(dis);
                it._int = MyIO.readInt16(dis);
                it._mov = MyIO.readInt16(dis);

                it.goal = MyIO.readByte(dis);
                it.state = MyIO.readByte(dis);
                it.stateLastTime = MyIO.readByte(dis);
                data.Add(it);
            }

            dis.Close();

        }

        public void display()
        {

            form.listBox11.Items.Clear();
            for (int i = 0; i < data.Count; i++)
            {
                Item t = (Item)(data[i]);
                form.listBox11.Items.Add(t.id + "," + t.name);
            }
            if (data.Count > 0)
            {
                form.listBox11.SelectedIndex = 0;

                displayatt(0);
            }
        }

        public void displayatt(int i)
        {
            if (i >= data.Count)
            {
                return;
            }
            Item t = (Item)data[i];

            form.textBox38.Text = "" + t.id;
            form.textBox39.Text = t.name;
            form.comboBox6.SelectedIndex = t.type;
            //MyIO.readByte(dis); // ignore icon
            form.textBox40.Text = "" + t.money;
            if (t.job == -1)
            {
                form.comboBox7.SelectedIndex = 3;
            }
            else
            {
                form.comboBox7.SelectedIndex = t.job;
            }
            form.textBox41.Text = "" + t.level;
            form.textBox42.Text = t.info;
            form.textBox43.Text = "" + t.pro;
            form.textBox44.Text = "" + t._hp;
            form.textBox45.Text = "" + t._mp;
            form.textBox46.Text = "" + t._pact;
            form.textBox47.Text = "" + t._mact;
            form.textBox48.Text = "" + t._pdef;
            form.textBox49.Text = "" + t._mdef;
            form.textBox50.Text = "" + t._pow;
            form.textBox51.Text = "" + t._pyh;
            form.textBox52.Text = "" + t._int;
            form.textBox53.Text = "" + t._mov;

            //goal = MyIO.readByte(dis);
            if (t.state == 5)
            {
                form.comboBox8.SelectedIndex = 1;
            }
            else
            {
                form.comboBox8.SelectedIndex = 0;
            }
            //stateLastTime = MyIO.readByte(dis);
        }

        public void up(int i)
        {
            if (i >= data.Count)
            {
                return;
            }
            Item t = (Item)data[i];
            t.id = SByte.Parse(form.textBox38.Text);
            t.name = form.textBox39.Text;
            t.type = (SByte)(form.comboBox6.SelectedIndex);
            //MyIO.readByte(dis); // ignore icon
            t.money = short.Parse(form.textBox40.Text);
            if (form.comboBox7.SelectedIndex == 3)
            {
                t.job = -1;
            }
            else
            {
                t.job = (SByte)form.comboBox7.SelectedIndex;
            }
            t.level = SByte.Parse(form.textBox41.Text);
            t.info = form.textBox42.Text;
            t.pro = SByte.Parse(form.textBox43.Text);
            t._hp = short.Parse(form.textBox44.Text);
            t._mp = short.Parse(form.textBox45.Text);
            t._pact = short.Parse(form.textBox46.Text);
            t._mact = short.Parse(form.textBox47.Text);
            t._pdef = short.Parse(form.textBox48.Text);
            t._mdef = short.Parse(form.textBox49.Text);
            t._pow = short.Parse(form.textBox50.Text);
            t._pyh = short.Parse(form.textBox51.Text);
            t._int = short.Parse(form.textBox52.Text);
            t._mov = short.Parse(form.textBox53.Text);

            //goal = MyIO.readByte(dis);
            if (form.comboBox8.SelectedIndex == 1)
            {
                t.state = 5;
            }
            else
            {
                t.state = 0;
            }
            //stateLastTime = MyIO.readByte(dis);
        }

        public void add()
        {

            Item t = new Item(null);
            t.id = SByte.Parse(form.textBox38.Text);
            t.name = form.textBox39.Text;
            t.type = (SByte)(form.comboBox6.SelectedIndex);
            //MyIO.readByte(dis); // ignore icon
            t.money = short.Parse(form.textBox40.Text);
            if (form.comboBox7.SelectedIndex == 3)
            {
                t.job = -1;
            }
            else
            {
                t.job = (SByte)form.comboBox7.SelectedIndex;
            }
            t.level = SByte.Parse(form.textBox41.Text);
            t.info = form.textBox42.Text;
            t.pro = SByte.Parse(form.textBox43.Text);
            t._hp = short.Parse(form.textBox44.Text);
            t._mp = short.Parse(form.textBox45.Text);
            t._pact = short.Parse(form.textBox46.Text);
            t._mact = short.Parse(form.textBox47.Text);
            t._pdef = short.Parse(form.textBox48.Text);
            t._mdef = short.Parse(form.textBox49.Text);
            t._pow = short.Parse(form.textBox50.Text);
            t._pyh = short.Parse(form.textBox51.Text);
            t._int = short.Parse(form.textBox52.Text);
            t._mov = short.Parse(form.textBox53.Text);

            //goal = MyIO.readByte(dis);
            if (form.comboBox8.SelectedIndex == 1)
            {
                t.state = 5;
            }
            else
            {
                t.state = 0;
            }
            data.Add(t);
        }
    }
}
